package com.xuecheng.manage_course.controller;

import com.xuecheng.api.course.CourseBaseControllerApi;
import com.xuecheng.framework.domain.course.CourseBase;
import com.xuecheng.framework.domain.course.response.AddCourseResult;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_course.service.CourseBaseService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/course/coursebase/")
public class CourseBaseController implements CourseBaseControllerApi {
    @Resource
    CourseBaseService courseBaseService;
    /**
     * 添加一个课程
     *
     * @param courseBase 课程实体类
     * @return 添加成功的id
     */
    @Override
    @PostMapping("/add")
    public AddCourseResult addCourseBase(@RequestBody @Valid CourseBase courseBase, BindingResult result) {
        if(result.hasErrors()){
           // 拦截到错误
            return new AddCourseResult(CommonCode.INVALID_JSON_PARAMETER,"");
        }
        return courseBaseService.addCourseBase(courseBase);
    }

    /**
     * 根据课程id查询一个课程信息
     *
     * @param courseId 课程id
     * @return 响应结果
     */
    @Override
    @GetMapping("/get/{courseId}")
    public CourseBase getCourseBaseById(@PathVariable("courseId") String courseId) {
        return courseBaseService.getCourseBaseById(courseId);
    }

    /**
     * 修改课程信息
     *
     * @param courseId 课程id
     * @param courseBase 课程实体类
     * @return 响应结果
     */
    @Override
    @PutMapping("/put/{courseId}")
    public ResponseResult updateCoursebase(@PathVariable("courseId") String courseId, @RequestBody CourseBase courseBase) {
        return courseBaseService.updateCoursebase(courseId,courseBase);
    }

}
